package com.fivefu.core.camera.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.base.web.controller.BaseController;
import com.fivefu.base.web.vo.ResultInfo;

import com.fivefu.core.camera.entity.DbMonitorLabel;
import com.fivefu.core.camera.services.*;
import com.fivefu.core.camera.vo.response.LabelPageList;
import com.fivefu.core.camera.vo.response.ProPageList;

import com.fivefu.core.data.vo.TreeVo;
import com.fivefu.core.module.auth.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.validation.Valid;
import java.util.List;


/**
 * <p>
 * 标签表 前端控制器
 * </p>
 *
 * @author
 * @since 2022-09-19 13:48:32
 */
@Api(tags = "标签管理")
@RestController
@RequestMapping("/db_monitor_label")
public class DbMonitorLabelController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(DbSceneEventController.class);

    @Autowired
    private DbMonitorLabelService dbMonitorLabelService;
    @Autowired
    private MonitorLabelService monitorLabelService;
    @Autowired
    private CameraTreeService cameraTreeService;

    @ApiOperation(value = "编辑标签",httpMethod = "POST",response = ResultInfo.class,notes = "编辑标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "labelId",value = "标签编号",paramType = "query",required = false, dataType = "Long"),
            @ApiImplicitParam(name = "labelName",value = "标签名称",paramType = "query",required = true, dataType = "String"),
    })
    @RequestMapping(path = "/editMonitorLabel",method = RequestMethod.POST)
    public ResultInfo querySceneDetail(Long labelId,String labelName, @RequestAttribute String userid){
        try {
            if (StrUtils.isEmpty(labelName)){
                return ResultInfo.renderError("标签名称不能为空");
            }

            //判断是否能修改
            if (labelId != null){
                DbMonitorLabel labelById = dbMonitorLabelService.queryLabelById(labelId);
                if (labelById != null && labelById.getEditable() == 0){
                    return ResultInfo.renderError("标签不能编辑");
                }
            }

            //判断标签名称是否重复
            DbMonitorLabel label = dbMonitorLabelService.queryMonitorLabelByLabelName(labelName);
            if (label != null){
                if (labelId == null || (labelId != null && !labelId.equals(label.getId()))) {
                    return ResultInfo.renderError("标签名称不能重复");
                }
            }

            Long id = dbMonitorLabelService.editLabelByLabelName(labelId, labelName, userid);
            if (labelId == null){
                return ResultInfo.renderSuccess(200,"添加标签成功",id);
            }else{
                return ResultInfo.renderSuccess("编辑标签成功");
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("编辑标签失败");
    }

    @ApiOperation(value = "删除标签",httpMethod = "POST",response = ResultInfo.class,notes = "删除标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "labelId",value = "标签id",paramType = "query",required = true, dataType = "Long"),
    })
    @RequestMapping(path = "/delMonitorLabel",method = RequestMethod.POST)
    public ResultInfo delMonitorLabel(Long labelId, @RequestAttribute String userid){
        try {
            if (labelId == null){
                return ResultInfo.renderError("标签id为空");
            }
            DbMonitorLabel label = dbMonitorLabelService.queryLabelById(labelId);
            if (label != null && label.getEditable() == 0){
                return ResultInfo.renderError("标签不能删除");
            }
            boolean flag = monitorLabelService.delMonitorLabelById(labelId,userid);
            if (flag) return ResultInfo.renderSuccess("删除标签成功");
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("删除标签失败");
    }

    @ApiOperation(value = "分页查询标签列表",httpMethod = "POST",response = ResultInfo.class,notes = "分页查询标签列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页数",paramType = "query",required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "limit",value = "数量",paramType = "query",required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "labelName",value = "标签名称",paramType = "query",required = false, dataType = "String"),
    })
    @RequestMapping(path = "/labelList",method = RequestMethod.POST)
    public ResultInfo labelList(Integer page,Integer limit,String labelName){
        try {
            Page<LabelPageList> pageListPage = monitorLabelService.queryLabelListByLabelName(page, limit, labelName);
            return ResultInfo.renderSuccess(0,"查询成功",pageListPage.getTotal(),pageListPage.getRecords());
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("分页查询标签列表失败");
    }

    @ApiOperation(value = "查询标签下探头列表",httpMethod = "POST",response = ResultInfo.class,notes = "查询标签下探头列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query",required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "数量", paramType = "query",required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "labelId", value = "标签编号", paramType = "query",required = true, dataType = "Long"),

            @ApiImplicitParam(name = "cameraCode", value = "探头编号", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraName", value = "探头类型", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraType", value = "标签名称", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraManufacturer", value = "厂商", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraAreaid", value = "所属区域", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraStreetid", value = "所属街道", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraStatus", value = "探头状态", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "onlineStatus", value = "在线状态", paramType = "query",required = false, dataType = "String"),
    })
    @RequestMapping(path = "/queryLabelMonList",method = RequestMethod.POST)
    public ResultInfo queryLabelMonList(Integer page,Integer limit,Long labelId){
        try {
            logger.info("查询增加探头列表接收的参数："+ request.getParameterMap());
            if (labelId == null){
                return ResultInfo.renderError("标签编号不能为空");
            }
            //判断当前页是否为空
            if (page == null){
                return ResultInfo.renderError("页码不能为空");
            }
            //判断每页显示条数是否为空
            if (limit == null){
                return ResultInfo.renderError("每页显示条数不能为空");
            }
            String cameraCode = StrUtils.isCheckNull(request.getParameter("cameraCode"));
            String cameraName = StrUtils.isCheckNull(request.getParameter("cameraName"));
            String cameraType = StrUtils.isCheckNull(request.getParameter("cameraType"));
            String cameraManufacturer = StrUtils.isCheckNull(request.getParameter("cameraManufacturer"));
            String cameraAreaid = StrUtils.isCheckNull(request.getParameter("cameraAreaid"));
            String cameraStreetid = StrUtils.isCheckNull(request.getParameter("cameraStreetid"));
            String cameraStatus = StrUtils.isCheckNull(request.getParameter("cameraStatus"));
            String onlineStatus = StrUtils.isCheckNull(request.getParameter("onlineStatus"));

            Page<ProPageList> list = monitorLabelService.queryLabelMonList(page,limit,labelId,cameraCode,cameraName,cameraType,cameraManufacturer,cameraAreaid,cameraStreetid,cameraStatus, onlineStatus);
            if (list != null && list.getSize() > 0){
                return ResultInfo.renderSuccess(0,"查询标签下探头列表成功",list.getTotal(),list.getRecords());
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("查询标签下探头列表失败");
    }

    @ApiOperation(value = "批量移除标签下探头",httpMethod = "POST",response = ResultInfo.class,notes = "批量移除标签下探头")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "labelId",value = "标签编号",paramType = "query",required = true, dataType = "Long"),
            @ApiImplicitParam(name = "cameraIds",value = "移除探头编号集合,,多个用,分隔",paramType = "query",required = true, dataType = "String"),
    })
    @RequestMapping(path = "/delMulMonitor",method = RequestMethod.POST)
    public ResultInfo delMulMonitor(String cameraIds, Long labelId, @RequestAttribute String userid){
        try {
            if (labelId == null){
                return ResultInfo.renderError("标签编号为空");
            }
            if (StrUtils.isEmpty(cameraIds)){
                return ResultInfo.renderError("探头id集合不能为空");
            }
            if (cameraIds.contains("，")){
                return ResultInfo.renderError("探头标识用英文逗号分开");
            }
            boolean flag = monitorLabelService.delMulMonitor(labelId, cameraIds,userid);
            if (flag) return ResultInfo.renderSuccess("批量移除标签下探头成功");
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("批量移除标签下探头失败");
    }

    @ApiOperation(value = "批量增加标签下探头",httpMethod = "POST",response = ResultInfo.class,notes = "批量增加标签下探头")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "labelId",value = "标签编号",paramType = "query",required = true, dataType = "Long"),
            @ApiImplicitParam(name = "cameraIds",value = "增加探头Id集合,多个用,分开",paramType = "query",required = true,dataType = "String"),
    })
    @RequestMapping(path = "/addMoniterLabel",method = RequestMethod.POST)
    public ResultInfo addMoniterLabel(String cameraIds,Long labelId, @RequestAttribute String userid){
        try {
            if (labelId == null){
                return ResultInfo.renderError("标签编号为空");
            }
            if (StrUtils.isEmpty(cameraIds)){
                return ResultInfo.renderError("探头编号不能为空");
            }
            if (cameraIds.contains("，")){
                return ResultInfo.renderError("多个探头编号用英文,分开");
            }
            monitorLabelService.saveBatchCameraLabel(labelId, cameraIds, userid);
            return ResultInfo.renderSuccess("批量增加标签下探头成功");
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("批量增加标签下探头失败");
    }

    @ApiOperation(value = "探头增加标签探头列表（排除已增加的探头）",httpMethod = "POST",response = ResultInfo.class,notes = "探头增加标签探头列表（排除已增加的探头）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query",required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "数量", paramType = "query",required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "labelId", value = "标签id", paramType = "query",required = true, dataType = "Long"),

            @ApiImplicitParam(name = "cameraCode", value = "探头编号", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraName", value = "探头名称", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraType", value = "探头类型", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraManufacturer", value = "厂商", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraAreaid", value = "所属区域", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraStreetid", value = "所属街道", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "cameraStatus", value = "探头状态", paramType = "query",required = false, dataType = "String"),
            @ApiImplicitParam(name = "onlineStatus", value = "在线状态", paramType = "query",required = false, dataType = "String"),
    })
    @RequestMapping(path = "/addMoniterList",method = RequestMethod.POST)
    public ResultInfo addMoniterList(Integer page,Integer limit,Long labelId){
        try {
            logger.info("探头增加标签探头列表（排除已增加的探头）："+ request.getParameterMap());
            if (labelId == null){
                return ResultInfo.renderError("场景编号不能为空");
            }
            //判断当前页是否为空
            if (page == null){
                return ResultInfo.renderError("当前页不能为空");
            }
            //判断每页显示条数是否为空
            if (limit == null){
                return ResultInfo.renderError("每页显示条数不能为空");
            }
            String cameraCode = StrUtils.isCheckNull(request.getParameter("cameraCode"));
            String cameraName = StrUtils.isCheckNull(request.getParameter("cameraName"));
            String cameraType = StrUtils.isCheckNull(request.getParameter("cameraType"));
            String cameraManufacturer = StrUtils.isCheckNull(request.getParameter("cameraManufacturer"));
            String cameraAreaid = StrUtils.isCheckNull(request.getParameter("cameraAreaid"));
            String cameraStreetid = StrUtils.isCheckNull(request.getParameter("cameraStreetid"));
            String cameraStatus = StrUtils.isCheckNull(request.getParameter("cameraStatus"));
            String onlineStatus = StrUtils.isCheckNull(request.getParameter("onlineStatus"));

            Page<ProPageList> list = monitorLabelService.queryMonitorNotInLabel(page,limit,labelId,cameraCode,cameraName,cameraType,cameraManufacturer,cameraAreaid,cameraStreetid,cameraStatus, onlineStatus);
            return ResultInfo.renderSuccess(0,"探头增加标签探头列表（排除已增加的探头）成功",list.getTotal(),list.getRecords());
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("探头增加标签探头列表（排除已增加的探头）失败");
    }

    @ApiOperation(value = "查询新增标签下探头树形结构",httpMethod = "POST",response = ResultInfo.class,notes = "查询新增标签下探头树形结构")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "查询类型(list,tree)",paramType = "query",required = true, dataType = "String"),
            @ApiImplicitParam(name = "labelId",value = "标签Id",paramType = "query",required = true, dataType = "Long")
    })
    @RequestMapping(path = "/queryLabelTreeList",method = RequestMethod.POST)
    public ResultInfo queryLabelTreeList(String type,Long labelId){
        logger.info("查询新增标签下探头树形结构接收的参数："+ JSON.toJSONString(request.getParameterMap()));
        try {
            if (StrUtils.isNull(type)){
                return ResultInfo.renderError("查询类型不能为空");
            }
            if (labelId == null){
                return ResultInfo.renderError("标签编号不能为空");
            }
            List<TreeVo> treeList = cameraTreeService.getAddMonitorRegionTreeByLabelId(type, labelId);
            return ResultInfo.renderSuccess(treeList);
        }catch (Exception e){
            logger.error(e.getMessage(), e);
        }
        return ResultInfo.renderError("查询新增标签下探头树形结构失败");
    }

}
